#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

vector<vector<int>> adj;
vector<int> dp_center, dp_leaf;

void dfs(int u, int parent) {
    vector<int> candidates;
    int max_leaf = -1;
    
    for (int v : adj[u]) {
        if (v == parent) continue;
        dfs(v, u);
        
        if (dp_leaf[v] != -1) 
            candidates.push_back(dp_leaf[v]);
        if (dp_center[v] != -1)
            max_leaf = max(max_leaf, dp_center[v] + 1);
    }
    
    // 更新dp_leaf[u]: u作为叶子，必须子树中存在中心
    dp_leaf[u] = (max_leaf != -1) ? (1 + max_leaf) : -1;
    
    // 更新dp_center[u]: 需要至少4个分支
    if (candidates.size() >= 4) {
        nth_element(candidates.begin(), candidates.begin()+3, candidates.end(), greater<int>());
        int sum = 1;
        for (int i = 0; i < 4; ++i) sum += candidates[i];
        dp_center[u] = sum;
    } else {
        dp_center[u] = -1;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int N;
    cin >> N;
    adj.resize(N+1);
    dp_center.assign(N+1, -1);
    dp_leaf.assign(N+1, -1);
    
    for (int i = 0; i < N-1; ++i) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    
    int ans = -1;
    for (int u = 1; u <= N; ++u) {
        dfs(u, -1);
        if (dp_center[u] != -1) ans = max(ans, dp_center[u]);
        if (dp_leaf[u] != -1 && dp_leaf[u] >= 5) ans = max(ans, dp_leaf[u]);
    }
    
    cout << ans << endl;
    return 0;
}